﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XProject.Util
{
   public class LunarDate
    {
       int day;
       int month;
       int year;
       int leapMonth;
       DateTime solarDate;
       LunarCalendar cal = new LunarCalendar();
       string[] canArr = new string[] { "Giáp", "Ất", "Bính", "Đinh", "Mậu", "Kỷ", "Canh", "Tân", "Nhâm", "Qúy" };
       string[] chiArr = new string[] { "Tý", "Sửu", "Dần", "Mão", "Thìn", "Tỵ", "Ngọ", "Mùi", "Thân", "Dậu", "Tuất", "Hợi" };
       public LunarDate() { }
       public LunarDate(DateTime date)
       {
           this.day = date.Day;
           this.month = date.Month;
           this.year = date.Year;
       }
       public int Day
       {
           get { return day; }
       }
       public int Month
       {
           get { return month; }
       }
       public int Year
       {
           get { return year; }
       }
       public int LeapMonth
       {
           get { return leapMonth; }
       }
       #region method
       public void SetSolarDate(DateTime date)
       {
           solarDate = date;
       }
       /// <summary>
       /// Convert solar day into lunar day
       /// </summary>
       /// <returns></returns>
       public LunarDate GetLunarDate()
       {
           int[] dmy = cal.convertSolar2Lunar(solarDate.Day, solarDate.Month, solarDate.Year, 7);
           this.day = dmy[0];
           this.month = dmy[1];
           this.year = dmy[2];
           return this;
           
       }
       public static DateTime GetSolarDate(LunarDate lunarDate)
       {
           DateTime solarDate = new DateTime();
           //int[] dmy = LunarCalendar.convertLunar2Solar(lunarDate.Day,lunarDate.Month,lunarDate.Year,,7);
          // solarDate = new DateTime(dmy[2],dmy[1],dmy[0]);
           return solarDate;
       }
       public string GetDayName()
       {
           LunarCalendar lc = new LunarCalendar();
           int days = lc.jdFromDate(solarDate.Day,solarDate.Month,solarDate.Year);          
           int can = (days+9) % 10;
           int chi = (days+1) % 12;
           return canArr[can] + " " + chiArr[chi];
       }
       public string GetMonthName()
       {
           GetLunarDate();
           int can = (Year * 12 + Month+3) % 10;
           int chi=(Month+1)%12;
           return canArr[can] + " " + chiArr[chi];
       }
       public string GetYearName()
       {
           GetLunarDate();
           int can = (Year +6) % 10;
           int chi = (Year  + 8) % 12;
           return canArr[can] + " " + chiArr[chi];
       }
       public override string ToString()
       {
           GetLunarDate();
           return this.Day.ToString()+"/"+this.Month.ToString()+"/"+this.Year.ToString();
       }
       #endregion
   }
}
